package com.share.sharelibrary.util;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.kedll.library.util.ToastUtil;
import com.tencent.connect.common.Constants;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * @author QiQi
 */
public class ShareSDKHelp {

    //    public UMShareAPI mShareAPI;

    private ILogin login;
    private FragmentActivity mContext;
    private QQUiListener uiListener;

    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI mWXApi;

    private Tencent mTencent;

    public ShareSDKHelp(FragmentActivity context) {
        mContext = context;
        init();
    }

    private void init() {
        //从Manifest中读取key
        String wxAppId = "wx90982b1634ca5f00";
        String wxAppSecret = "";

        //0268a7cd5257711b36e5f9b9f51c633d
        String QQ_APP_ID = "101512273";
        String QQ_APP_KEY = "15d27f9d46118162c9863576cd7cfbde";

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        mWXApi = WXAPIFactory.createWXAPI(mContext, wxAppId, true);
        // 将应用的appId注册到微信
        mWXApi.registerApp(wxAppId);
        // Tencent类是SDK的主要实现类，开发者可通过Tencent类访问腾讯开放的OpenAPI。
        // 其中APP_ID是分配给第三方应用的appid，类型为String。
        mTencent = Tencent.createInstance(QQ_APP_ID, mContext.getApplicationContext());
        uiListener = new QQUiListener();
    }

    public void setLogin(ILogin login) {
        this.login = login;
    }

    public void onResult(int requestCode, int resultCode, Intent data) {
        Log.d("ShareSDKHelp", "-->onActivityResult " + requestCode + " resultCode=" + resultCode);
        if (requestCode == Constants.REQUEST_LOGIN ||
                requestCode == Constants.REQUEST_APPBAR) {
            Tencent.onActivityResultData(requestCode, resultCode, data, uiListener);
        }
    }

    /**
     * 用户登录
     *
     * @param platform
     */
    public void doOauth(final SHARE_MEDIA platform) {
        if (platform.equals(SHARE_MEDIA.QQ)) {
            if (!mTencent.isSessionValid()) {
                mTencent.login(mContext, "all", uiListener);
            }

        } else if (platform.equals(SHARE_MEDIA.WEIXIN)) {
            if (mWXApi == null) {
                return;
            }
            final SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk_login_zp";
            mWXApi.sendReq(req);
        }


    }

    public void onDestroy() {
        mContext = null;
        mWXApi = null;
        uiListener = null;
        login = null;
    }

    /**
     * Created by slj on 2015/5/26.
     */
    public interface ILogin {
        /**
         * 获取用户资料回调
         *
         * @param access_token
         */
        void onLogin(String access_token);
    }

    class QQUiListener implements IUiListener {
        @Override
        public void onComplete(Object response) {
            if (null == response) {
                ToastUtil.showToast("返回为空" + "登录失败");
                return;
            }
            JSONObject jsonResponse = (JSONObject) response;
            try {
                login.onLogin(jsonResponse.getString("access_token"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(UiError uiError) {
            Log.e("-*--*", uiError.errorMessage);
        }

        @Override
        public void onCancel() {
            Log.e("-*--*", "");
        }
    }

}
